package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.x.erp.model.InsuranceGoodsVoucher;
import com.x.erp.model.InsuranceOrder;
import com.x.erp.rpc.InsuranceGoodsVoucherRpcService;
import com.x.erp.util.ConstantState;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 延保商品使用券Controller
 *
 * @author pansong
 */
@RestController
@RequestMapping(value = "/insuranceGoodsVoucher")
public class InsuranceGoodsVoucherController extends BaseController {

	@Reference
	private InsuranceGoodsVoucherRpcService insuranceGoodsVoucherRpcService;

	/**
	 * 获取延保商品使用券列表
	 *
	 * @param insuranceGoodsVoucher
	 * @return
	 */
	@RequestMapping(value = "/selectVoucherList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectVoucherList(InsuranceGoodsVoucher insuranceGoodsVoucher) {
		if(StringUtils.isEmpty(insuranceGoodsVoucher.getOrderId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "延保订单Id不能为空");
		}
		List<InsuranceGoodsVoucher> list = insuranceGoodsVoucherRpcService.selectVoucherList(insuranceGoodsVoucher);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", list);
	}

	/**
	 * 获取延保商品使用券列表数量
	 *
	 * @param insuranceGoodsVoucher
	 * @return
	 */
	@RequestMapping(value = "/selectVoucherNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectVoucherNum(InsuranceGoodsVoucher insuranceGoodsVoucher) {
		if(StringUtils.isEmpty(insuranceGoodsVoucher.getOrderId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "延保订单Id不能为空");
		}
		int res = insuranceGoodsVoucherRpcService.selectVoucherNum(insuranceGoodsVoucher);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 修改多条延保商品使用券（核销）
	 *
	 * @param insuranceOrder
	 * @return
	 */
	@RequestMapping(value = "/updateVoucherList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel updateVoucherList(InsuranceOrder insuranceOrder, String voucherJson) {
		if(StringUtils.isEmpty(insuranceOrder.getOrderId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "延保订单Id不能为空");
		}
		List<InsuranceGoodsVoucher> insuranceGoodsVouchers = JSON.parseArray(voucherJson, InsuranceGoodsVoucher.class);
		if (insuranceGoodsVouchers == null || insuranceGoodsVouchers.size() == 0) {
			return new ResultModel(ResultCode.RESULT_FAIL, "使用券集合不能为空");
		}
		int temp = insuranceGoodsVoucherRpcService.updateVoucherList(insuranceOrder, insuranceGoodsVouchers);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "核销成功", temp);
	}
}
